// 用户收益
import { PACKAGE_OWNER_PAGE } from '../../../constants/route';
import { WITHDRAW_TYPE } from '../../../constants/common';
import { getUserEarningInfo } from '../../../api/usr';
const { navTo, showLoading, hideLoading, hidePullDownLoading } = require('../../../common/env');

Page({
    data: {
        ownerEarn: {
            availableAmount: 0,
            pendingAmount: 0,
            withdrawedAmount: 0,
        },
    },
    
    onShow: async function () {
        // 每次展示都更新收益数据
        await this.getEarningInfo();
    },

    // 下拉刷新
    onPullDownRefresh: async function () {
        await this.getEarningInfo();
        hidePullDownLoading();
    },

    // 获取用户信息
    getEarningInfo: async function () {
        showLoading('加载中...');
        const info = await getUserEarningInfo();
        console.log('UserEarnings => 用户收入信息为', info);
        this.setData({
            ownerEarn: info,
        });
        hideLoading();
    },

    // 收银详情
    goToRecordList: function () {
        navTo(PACKAGE_OWNER_PAGE.ownerEarningsLog);
    },

    // 提现记录
    goToWithdrawLog: function () {
        navTo(PACKAGE_OWNER_PAGE.ownerWithdrawLog);
    },

    // 提现到零钱
    withdrawToChange: function () {
        navTo(PACKAGE_OWNER_PAGE.ownerWithdraw, {
            type: WITHDRAW_TYPE.change,
            amount: this.data.ownerEarn.availableAmount,
        })
    },

    // 提现到银行卡
    withdrawToCard: function () {
        navTo(PACKAGE_OWNER_PAGE.ownerWithdraw, {
            type: WITHDRAW_TYPE.bank,
            amount: this.data.ownerEarn.availableAmount,
        })
    },

})